﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace LicPdaService
{
    public class PDAXmlPutawayProcess
    {
        /// <summary>
        /// 4.1成品上架XML获取库位 Add By Quinn.Ling 201703
        /// DataSource:Inventory
        /// </summary>

        public static string GetPDAPutawayData(XmlDocument doc)
        {
            #region 通用变量定义
            string docResult = string.Empty; //返回值
            string userid = string.Empty;
            string whseid = string.Empty;
            string serial = string.Empty;
            string asn = string.Empty;
            string sku = string.Empty;
            string pre_qty = string.Empty;
            string qty = string.Empty;
            string storage = string.Empty;
            string return_status = "OK";
            List<string> sqllists = new List<string>();
            string strSql = string.Empty;

            XmlDocument docreturn = new XmlDocument();//返回报文
            XmlDeclaration dec = docreturn.CreateXmlDeclaration("1.0", "utf-8", null);//建立Xml的定义声明  
            docreturn.AppendChild(dec);
            XmlElement rootResult = docreturn.CreateElement("NewDataSet"); //创建根节点
            #endregion

            try
            {
                #region
                //App.ws = new Fcl.WSOP.sqldbop();
                //App.ws.Init(System.Configuration.ConfigurationManager.ConnectionStrings["adpwmsConnectionString"].ConnectionString);
                XmlNode root = doc.DocumentElement; // 报文根节点
                XmlNodeList rootlist = root.ChildNodes;
                #endregion

                for (int i = 0; i < rootlist.Count; i++)
                {
                    #region 参数源
                    XmlNode nodePutaway = rootlist[i];
                    userid = nodePutaway.SelectSingleNode("userid").InnerText;
                    whseid = nodePutaway.SelectSingleNode("whseid").InnerText;
                    asn = nodePutaway.SelectSingleNode("asn").InnerText;
                    serial = nodePutaway.SelectSingleNode("serial").InnerText;
                    sku = nodePutaway.SelectSingleNode("sku").InnerText;
                    pre_qty = nodePutaway.SelectSingleNode("pre_qty").InnerText;
                    string lscustomer_code = "";
                    string lstest = "";
                    //3获取库位
                    //3.1补充信息customer_code,test
                    strSql = "select customer_code from Receipt where receiptkey='" + asn + "' and whseid='" + whseid + "'";

                    DataTable ldt_customer_code = App.GetDataTable(App.DBconWMS, strSql, "Sku");

                    if (ldt_customer_code.Rows.Count > 0)
                    {
                        lscustomer_code = ldt_customer_code.Rows[0]["customer_code"].ToString();
                    }
                    strSql = "select test from sku where sku='" + sku + "'  and whseid='" + whseid + "'";
                    DataTable ldt_test = App.GetDataTable(App.DBconWMS, strSql, "Sku");
                    if (ldt_test.Rows.Count > 0)
                    {
                        lstest = ldt_test.Rows[0]["test"].ToString();
                    }
                    DataTable ldt_sj = new DataTable();
                    ldt_sj.Columns.Add("asn");
                    ldt_sj.Columns.Add("sku");
                    ldt_sj.Columns.Add("lottable02");
                    ldt_sj.Columns.Add("lottable01");
                    ldt_sj.Columns.Add("shipkey");
                    ldt_sj.Columns.Add("qty");
                    ldt_sj.Columns.Add("master_label");
                    ldt_sj.Columns.Add("userid");
                    ldt_sj.Columns.Add("whseid");
                    ldt_sj.Columns.Add("storage");
                    ldt_sj.Columns.Add("check");
                    ldt_sj.Columns.Add("lpn");
                    ldt_sj.Columns.Add("test");
                    ldt_sj.Columns.Add("customer_code");
                    DataRow ldr = ldt_sj.NewRow();
                    ldr["asn"] = asn;
                    ldr["sku"] = sku;
                    ldr["lottable02"] = "";
                    ldr["lottable01"] = "";
                    ldr["shipkey"] = "";
                    ldr["qty"] = pre_qty;
                    ldr["master_label"] = "";
                    ldr["userid"] = userid;
                    ldr["whseid"] = whseid;
                    ldr["check"] = "";
                    ldr["lpn"] = "";
                    ldr["lpn"] = lstest;
                    ldr["customer_code"] = lscustomer_code;
                    ldt_sj.Rows.Add(ldr);


                    string lskw = App.GetPutAway(ldt_sj);
                    #endregion

                    if (lskw == "")
                    {
                        storage = lskw; //暂定库位未知--待确认
                        //storage = dt.Rows[0]["storage"].ToString(); //库位信息

                        #region 构建XML报文
                        XmlNode asn_result = docreturn.CreateElement("putaway_return");

                        XmlElement useridResult = docreturn.CreateElement("userid");
                        useridResult.InnerText = userid;
                        asn_result.AppendChild(useridResult);

                        XmlElement whseidResult = docreturn.CreateElement("whseid");
                        whseidResult.InnerText = whseid;
                        asn_result.AppendChild(whseidResult);

                        XmlElement asnResult = docreturn.CreateElement("asn");
                        asnResult.InnerText = asn;
                        asn_result.AppendChild(asnResult);

                        XmlElement serialResult = docreturn.CreateElement("serial");
                        serialResult.InnerText = serial;
                        asn_result.AppendChild(serialResult);

                        XmlElement skuResult = docreturn.CreateElement("sku");
                        skuResult.InnerText = sku;
                        asn_result.AppendChild(skuResult);

                        XmlElement qtyResult = docreturn.CreateElement("qty");
                        qtyResult.InnerText = pre_qty;
                        asn_result.AppendChild(qtyResult);

                        XmlElement storageResult = docreturn.CreateElement("storage");
                        storageResult.InnerText = storage;
                        asn_result.AppendChild(storageResult);

                        XmlElement return_statusResult = docreturn.CreateElement("return_status");
                        return_statusResult.InnerText = return_status;
                        asn_result.AppendChild(return_statusResult);

                        rootResult.AppendChild(asn_result);
                        #endregion
                    }
                    else
                    {
                        #region 构建XML报文

                        storage = "UNKNOW";
                        XmlNode asn_result = docreturn.CreateElement("asn_return");

                        XmlElement useridResult = docreturn.CreateElement("userid");
                        useridResult.InnerText = userid;
                        asn_result.AppendChild(useridResult);

                        XmlElement whseidResult = docreturn.CreateElement("whseid");
                        whseidResult.InnerText = whseid;
                        asn_result.AppendChild(whseidResult);

                        XmlElement asnResult = docreturn.CreateElement("asn");
                        asnResult.InnerText = asn;
                        asn_result.AppendChild(asnResult);

                        XmlElement serialResult = docreturn.CreateElement("serial");
                        serialResult.InnerText = serial;
                        asn_result.AppendChild(serialResult);

                        XmlElement skuResult = docreturn.CreateElement("sku");
                        skuResult.InnerText = sku;
                        asn_result.AppendChild(skuResult);

                        XmlElement qtyResult = docreturn.CreateElement("qty");
                        qtyResult.InnerText = qty;
                        asn_result.AppendChild(qtyResult);

                        XmlElement storageResult = docreturn.CreateElement("storage");
                        storageResult.InnerText = storage;
                        asn_result.AppendChild(storageResult);

                        XmlElement return_statusResult = docreturn.CreateElement("return_status");
                        return_statusResult.InnerText = return_status;
                        asn_result.AppendChild(return_statusResult);

                        rootResult.AppendChild(asn_result);
                        #endregion
                    }
                }
            }
            catch (Exception ex)
            {
                App.Log("PDAXmlPutawayProcess.cs-1", ex.Message.ToString());
                //throw new Exception("异常源：PDAXmlPrePickProcess:GetPDAPrePickData;" + "  " + ex.Message);
            }
            docreturn.AppendChild(rootResult);
            return docreturn.InnerXml;

        }
    }
}
